package com.core.gui.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.core.dao.DAOException;
import com.core.entities.Contact;
import com.core.entities.OnlineAccount;
import com.core.entities.Work;
import com.core.gui.helper.UserInfo;

public class AddContact extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private final JLabel lblNewLabel = new JLabel("First Name :");
	public final JTextField firstNameTextField = new JTextField();
	private final JLabel lblSecondName = new JLabel("Second Name :");
	public final JTextField secondNameTextField = new JTextField();
	private final JLabel lblThirdName = new JLabel("Third Name :");
	public final JTextField thirdNameTextField = new JTextField();
	private final JLabel lblLastName = new JLabel("Last Name :");
	public final JTextField lastNameTextField = new JTextField();
	private final JLabel lblAddress = new JLabel("Address :");
	public final JTextArea addressTextArea = new JTextArea();
	private final JLabel lblCountry = new JLabel("Country :");
	public final JComboBox countryComboBox = new JComboBox(UserInfo
			.getInstance().getService().getCountrySrv().getCountriesNames()
			.toArray());
	private final JLabel lblStat = new JLabel("Stat :");
	public final JComboBox stateComboBox = new JComboBox(UserInfo
			.getInstance().getService().getStateSrv()
			.getStateByCountry(countryComboBox.getSelectedItem().toString())
			.toArray());
	private final JLabel lblCity = new JLabel("City :");
	public JComboBox cityComboBox = new JComboBox(UserInfo.getInstance()
			.getService().getCitySrv()
			.getCities(stateComboBox.getSelectedItem().toString()).toArray());
	private final JLabel lblNewLabel_1 = new JLabel("Land Phone :");
	public final JTextField landPhoneTextField = new JTextField();
	private final JLabel lblMobilePhone = new JLabel("Mobile Phone :");
	public final JTextField mobilePhoneTextField = new JTextField();
	private final JLabel lblEmail = new JLabel("E-Mail :");
	public final JTextField emailTextField = new JTextField();
	private final JLabel lblWebsite = new JLabel("Website :");
	public final JTextField websiteTextField = new JTextField();
	private final JLabel lblFacebook = new JLabel("Facebook :");
	public final JTextField facebookTextField = new JTextField();
	public final JLabel lblNewLabel_2 = new JLabel("confirm");
	public final JLabel lblNewLabel_3 = new JLabel("Twitter :");
	public final JTextField twitterTextField = new JTextField();
	public final JLabel lblConfirm = new JLabel(" confirm");
	private final JLabel lblGoogle = new JLabel("Google :");
	public final JTextField googleTextField = new JTextField();
	public final JLabel label = new JLabel(" confirm");
	private final JLabel lblJob = new JLabel("Job :");
	public final JTextField jobTextField = new JTextField();
	private final JLabel lblCompanyName = new JLabel("Company Name :");
	public final JTextField companyTextField = new JTextField();
	private final JLabel lblWorkLandPhone = new JLabel("Work Land Phone :");
	public final JTextField workLandPhoneTextField = new JTextField();
	private final JLabel lblWorkMobilePhone = new JLabel("Work Mobile Phone :");
	public final JTextField workMobilePhoneTextField = new JTextField();
	private final JLabel lblWorkEmail = new JLabel("Work E-Mail :");
	public final JTextField workEmailTextField = new JTextField();
	private final JLabel lblWorkWebsite = new JLabel("Work Website :");
	public final JTextField workWebsiteTextField = new JTextField();
	private final JLabel lblWorkCountry = new JLabel("Work Country :");
	public final JComboBox workCountryComboBox = new JComboBox(UserInfo
			.getInstance().getService().getCountrySrv().getCountriesNames()
			.toArray());
	private final JLabel lblWorkState = new JLabel("Work State :");
	public final JComboBox workStateComboBox = new JComboBox(UserInfo
			.getInstance().getService().getStateSrv()
			.getStateByCountry(countryComboBox.getSelectedItem().toString())
			.toArray());
	private final JLabel lblWorkCity = new JLabel("Work City  :");
	public JComboBox workCityComboBox = new JComboBox(UserInfo.getInstance()
			.getService().getCitySrv()
			.getCities(stateComboBox.getSelectedItem().toString()).toArray());
	public final JTextField workGoogleTextField = new JTextField();
	public final JTextField workTwitterTextField = new JTextField();
	public final JTextField workFacebookTextField = new JTextField();
	private final JLabel lblWorkFacebook = new JLabel("Work Facebook :");
	private final JLabel lblWorkTwitter = new JLabel("Work Twitter :");
	private final JLabel lblWorkGoogle = new JLabel("Work Google :");
	public final JLabel label_4 = new JLabel(" confirm");
	public final JLabel label_5 = new JLabel(" confirm");
	public final JLabel label_6 = new JLabel("confirm");
	private final JLabel lblCatagory = new JLabel("Catagory :");
	public final JComboBox catagoryComboBox = new JComboBox(UserInfo
			.getInstance().getService().getCatagorySrv().getCatagoryNames()
			.toArray());
	
	public final JButton okButton = new JButton("OK");
	public final JButton updateButton = new JButton("Update");

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AddContact dialog = new AddContact();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AddContact() {
		workWebsiteTextField.setBounds(87, 367, 245, 20);
		workWebsiteTextField.setColumns(10);
		workEmailTextField.setBounds(83, 342, 249, 20);
		workEmailTextField.setColumns(10);
		workMobilePhoneTextField.setBounds(116, 317, 194, 20);
		workMobilePhoneTextField.setColumns(10);
		workLandPhoneTextField.setBounds(106, 292, 194, 20);
		workLandPhoneTextField.setColumns(10);
		companyTextField.setBounds(96, 267, 236, 20);
		companyTextField.setColumns(10);
		jobTextField.setBounds(35, 239, 281, 20);
		jobTextField.setColumns(10);
		googleTextField.setBounds(60, 214, 272, 20);
		googleTextField.setColumns(10);
		twitterTextField.setBounds(60, 189, 272, 20);
		twitterTextField.setColumns(10);
		facebookTextField.setBounds(68, 164, 264, 20);
		facebookTextField.setColumns(10);
		websiteTextField.setBounds(58, 139, 274, 20);
		websiteTextField.setColumns(10);
		emailTextField.setBounds(48, 114, 281, 20);
		emailTextField.setColumns(10);
		mobilePhoneTextField.setBounds(329, 89, 168, 20);
		mobilePhoneTextField.setColumns(10);
		landPhoneTextField.setBounds(83, 89, 168, 20);
		landPhoneTextField.setColumns(10);
		lastNameTextField.setBounds(601, 8, 99, 20);
		lastNameTextField.setColumns(10);
		thirdNameTextField.setBounds(428, 8, 105, 20);
		thirdNameTextField.setColumns(10);
		secondNameTextField.setBounds(256, 8, 99, 20);
		secondNameTextField.setColumns(10);
		firstNameTextField.setBounds(70, 8, 99, 20);
		firstNameTextField.setColumns(10);
		setBounds(100, 100, 758, 582);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		lblNewLabel.setBounds(10, 11, 58, 14);

		contentPanel.add(lblNewLabel);

		contentPanel.add(firstNameTextField);
		lblSecondName.setBounds(179, 11, 72, 14);

		contentPanel.add(lblSecondName);

		contentPanel.add(secondNameTextField);
		lblThirdName.setBounds(365, 11, 61, 14);

		contentPanel.add(lblThirdName);

		contentPanel.add(thirdNameTextField);
		lblLastName.setBounds(543, 11, 57, 14);

		contentPanel.add(lblLastName);

		contentPanel.add(lastNameTextField);
		lblAddress.setBounds(10, 36, 46, 14);

		contentPanel.add(lblAddress);
		addressTextArea.setBounds(60, 36, 235, 37);

		contentPanel.add(addressTextArea);
		lblCountry.setBounds(305, 39, 46, 14);
		{

		}
		contentPanel.add(lblCountry);
		countryComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String countryName = ((JComboBox) e.getSource())
						.getSelectedItem().toString();

				stateComboBox.removeAllItems();
				for (String item : UserInfo.getInstance().getService()
						.getStateSrv().getStateByCountry(countryName))
					stateComboBox.addItem(item);
				repaint();
			}
		});

		countryComboBox.setBounds(353, 33, 152, 20);

		contentPanel.add(countryComboBox);
		lblStat.setBounds(515, 36, 27, 14);

		contentPanel.add(lblStat);
		stateComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String stateName = ((JComboBox) e.getSource())
						.getSelectedItem().toString();

				cityComboBox.removeAllItems();
				for (String item : UserInfo.getInstance().getService()
						.getCitySrv().getCities(stateName))
					cityComboBox.addItem(item);
				repaint();
			}
		});

		stateComboBox.setBounds(543, 33, 157, 20);

		contentPanel.add(stateComboBox);
		lblCity.setBounds(305, 64, 27, 14);

		contentPanel.add(lblCity);

		cityComboBox.setBounds(334, 61, 171, 20);

		contentPanel.add(cityComboBox);
		lblNewLabel_1.setBounds(10, 92, 63, 14);

		contentPanel.add(lblNewLabel_1);

		contentPanel.add(landPhoneTextField);
		lblMobilePhone.setBounds(256, 92, 70, 14);

		contentPanel.add(lblMobilePhone);

		contentPanel.add(mobilePhoneTextField);
		lblEmail.setBounds(10, 117, 35, 14);

		contentPanel.add(lblEmail);

		contentPanel.add(emailTextField);
		lblWebsite.setBounds(10, 142, 46, 14);

		contentPanel.add(lblWebsite);

		contentPanel.add(websiteTextField);
		lblFacebook.setBounds(10, 167, 53, 14);

		contentPanel.add(lblFacebook);

		contentPanel.add(facebookTextField);
		lblNewLabel_2.setBounds(336, 167, 46, 14);

		contentPanel.add(lblNewLabel_2);
		lblNewLabel_3.setBounds(10, 192, 46, 14);

		contentPanel.add(lblNewLabel_3);

		contentPanel.add(twitterTextField);
		lblConfirm.setBounds(334, 192, 46, 14);

		contentPanel.add(lblConfirm);
		lblGoogle.setBounds(10, 217, 40, 14);

		contentPanel.add(lblGoogle);

		contentPanel.add(googleTextField);
		label.setBounds(334, 217, 46, 14);

		contentPanel.add(label);
		lblJob.setBounds(10, 242, 27, 14);

		contentPanel.add(lblJob);

		contentPanel.add(jobTextField);
		lblCompanyName.setBounds(10, 270, 82, 14);

		contentPanel.add(lblCompanyName);

		contentPanel.add(companyTextField);
		lblWorkLandPhone.setBounds(10, 295, 91, 14);

		contentPanel.add(lblWorkLandPhone);

		contentPanel.add(workLandPhoneTextField);
		lblWorkMobilePhone.setBounds(10, 320, 98, 14);

		contentPanel.add(lblWorkMobilePhone);

		contentPanel.add(workMobilePhoneTextField);
		lblWorkEmail.setBounds(10, 345, 63, 14);

		contentPanel.add(lblWorkEmail);

		contentPanel.add(workEmailTextField);
		lblWorkWebsite.setBounds(10, 370, 74, 14);

		contentPanel.add(lblWorkWebsite);

		contentPanel.add(workWebsiteTextField);
		lblWorkCountry.setBounds(10, 395, 74, 14);

		contentPanel.add(lblWorkCountry);
		workCountryComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String countryName = ((JComboBox) e.getSource())
						.getSelectedItem().toString();

				workStateComboBox.removeAllItems();
				for (String item : UserInfo.getInstance().getService()
						.getStateSrv().getStateByCountry(countryName))
					workStateComboBox.addItem(item);
				repaint();
			}
		});

		workCountryComboBox.setBounds(88, 392, 144, 20);

		contentPanel.add(workCountryComboBox);
		lblWorkState.setBounds(256, 395, 61, 14);

		contentPanel.add(lblWorkState);
		workStateComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String stateName = ((JComboBox) e.getSource())
						.getSelectedItem().toString();

				workCityComboBox.removeAllItems();
				for (String item : UserInfo.getInstance().getService()
						.getCitySrv().getCities(stateName))
					workCityComboBox.addItem(item);
				repaint();
			}
		});

		workStateComboBox.setBounds(318, 392, 152, 20);

		contentPanel.add(workStateComboBox);
		lblWorkCity.setBounds(480, 395, 57, 14);

		contentPanel.add(lblWorkCity);

		workCityComboBox.setBounds(543, 392, 144, 20);

		contentPanel.add(workCityComboBox);
		workGoogleTextField.setColumns(10);
		workGoogleTextField.setBounds(80, 470, 272, 20);

		contentPanel.add(workGoogleTextField);
		workTwitterTextField.setColumns(10);
		workTwitterTextField.setBounds(80, 445, 272, 20);

		contentPanel.add(workTwitterTextField);
		workFacebookTextField.setColumns(10);
		workFacebookTextField.setBounds(93, 420, 264, 20);

		contentPanel.add(workFacebookTextField);
		lblWorkFacebook.setBounds(10, 423, 81, 14);

		contentPanel.add(lblWorkFacebook);
		lblWorkTwitter.setBounds(10, 448, 69, 14);

		contentPanel.add(lblWorkTwitter);
		lblWorkGoogle.setBounds(10, 473, 68, 14);

		contentPanel.add(lblWorkGoogle);
		label_4.setBounds(354, 473, 46, 14);

		contentPanel.add(label_4);
		label_5.setBounds(354, 448, 46, 14);

		contentPanel.add(label_5);
		label_6.setBounds(360, 423, 46, 14);

		contentPanel.add(label_6);

		lblCatagory.setBounds(515, 92, 52, 14);
		contentPanel.add(lblCatagory);

		catagoryComboBox.setBounds(572, 89, 128, 20);
		contentPanel.add(catagoryComboBox);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBounds(0, 507, 742, 37);
			contentPanel.add(buttonPane);
			{
				okButton.setBounds(620, 5, 47, 23);
				
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {

						try {
							UserInfo.getInstance()
									.getService()
									.getContactSrv()
									.registe(
											prepareWorkOnlineAccountObject(),
											prepareWorkObject(),
											prepareContactOnlineAccountObject(),
											prepareContactObject());
						} catch (DAOException e1) {
							e1.printStackTrace();
						}
						dispose();

					}
				});
				buttonPane.setLayout(null);
				{
					updateButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							ContactSearch contactSearch = new ContactSearch();
							
							contactSearch.updateContact();
//							UserInfo.getInstance().getService().getOnlineAccountSrv().getOnlineAccountDAO()
//							.update(prepareWorkOnlineAccountObject());
//							
//							UserInfo.getInstance().getService().getWorkSrv().getWorkDAO().update(prepareWorkObject());
//							
//							UserInfo.getInstance().getService().getOnlineAccountSrv().getOnlineAccountDAO()
//							.update(prepareContactOnlineAccountObject());
						}
					});
					updateButton.setBounds(600, 5, 67, 23);
					buttonPane.add(updateButton);
					updateButton.setVisible(false);
				}
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setBounds(672, 5, 65, 23);
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {

						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		catagoryComboBox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				((JComboBox) e.getSource()).getSelectedItem().toString();
			}
		});
	}


	private OnlineAccount prepareWorkOnlineAccountObject() throws DAOException {
		OnlineAccount workOnlineAccount = new OnlineAccount();

		workOnlineAccount.setFacebook(workFacebookTextField.getText());
		workOnlineAccount.setTwitter(workTwitterTextField.getText());
		workOnlineAccount.setGoogle(workGoogleTextField.getText());
		return workOnlineAccount;
	}

	private OnlineAccount prepareContactOnlineAccountObject()
			throws DAOException {
		OnlineAccount contactOnlineAccount = new OnlineAccount();

		contactOnlineAccount.setFacebook(facebookTextField.getText());
		contactOnlineAccount.setTwitter(twitterTextField.getText());
		contactOnlineAccount.setGoogle(googleTextField.getText());

		return contactOnlineAccount;
	}

	private Work prepareWorkObject() throws DAOException {
		Work work = new Work();

		work.setCompnyName(companyTextField.getText());
		work.setLandPhone(workLandPhoneTextField.getText());
		work.setMobilePhone(workMobilePhoneTextField.getText());
		work.setEmail(workEmailTextField.getText());
		work.setWebsite(workWebsiteTextField.getText());

		work.setCityId(UserInfo.getInstance().getService().getCitySrv()
				.getCityPK(workCityComboBox.getSelectedItem().toString()));

		return work;
	}

	public Contact prepareContactObject() {
		Contact contact = new Contact();

		contact.setFirstName(firstNameTextField.getText());
		contact.setSecondName(secondNameTextField.getText());
		contact.setThirdName(thirdNameTextField.getText());
		contact.setLastName(lastNameTextField.getText());
		contact.setAddress(addressTextArea.getText());

		contact.setCityId(UserInfo.getInstance().getService().getCitySrv()
				.getCityPK(cityComboBox.getSelectedItem().toString()));

		contact.setCatagoryId(UserInfo.getInstance().getService()
				.getCatagorySrv()
				.getCatagoryPK(catagoryComboBox.getSelectedItem().toString()));

		contact.setMobileNumber(mobilePhoneTextField.getText());
		contact.setLandPhone(landPhoneTextField.getText());
		contact.setEmail(emailTextField.getText());
		contact.setWebsite(websiteTextField.getText());
		contact.setJob(jobTextField.getText());

		return contact;

	}
}
